package com.example.toolbar.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.image.AbImageLoader;
import com.example.toolbar.R;
import com.example.toolbar.module.Article;
import com.lidroid.xutils.BitmapUtils;

/**
 * © 2012 amsoft.cn 名称：ArticleListAdapter 描述：文章对象自定义Adapter例子
 * 
 * @author 还如一梦中
 * @date 2011-11-8
 * @version
 */
public class ArticleListAdapter extends BaseAdapter {
	String pre = "http://tr.zzapi.gson.cn/";
	private Context mContext;
	// 列表展现的数据
	private List<Article> mList;
	// 图片下载器
	private AbImageLoader mAbImageLoader = null;
	BitmapUtils bitmapUtils;

	/**
	 * 构造方法
	 * 
	 * @param context
	 * @param list
	 *            列表展现的数据
	 */
	public ArticleListAdapter(Context context, List<Article> list) {
		mContext = context;
		mList = list;
		// 图片下载器
		mAbImageLoader = new AbImageLoader(mContext);
		mAbImageLoader.setLoadingImage(R.drawable.ic_load);
		mAbImageLoader.setErrorImage(R.drawable.ic_load);
		mAbImageLoader.setEmptyImage(R.drawable.ic_load);
		bitmapUtils = new BitmapUtils(mContext);
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (mList.get(position).getImageUrl().length == 1
				&& "".equals(mList.get(position).getImageUrl()[0])) {
			return 0;
		} else if (mList.get(position).getImageUrl().length == 1
				|| mList.get(position).getImageUrl().length == 2) {
			return 1;
		} else if (mList.get(position).getImageUrl().length >= 3) {
			return 2;
		} else {
			return 0;

		}
	}

	@Override
	public int getCount() {
		if (mList == null) {
			return 0;
		}
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		if (getItemViewType(position) == 0) {
			View view = inflater.inflate(R.layout.news_item3, null);
			TextView title = (TextView) view.findViewById(R.id.title);
			TextView source = (TextView) view.findViewById(R.id.source1);
			TextView comNum = (TextView) view.findViewById(R.id.comNum);
			TextView time = (TextView) view.findViewById(R.id.time);
			Article curr_new = mList.get(position);
			title.setText(curr_new.getTitle());
			source.setText(curr_new.getSource());
			comNum.setText(curr_new.getCommentNum());
			time.setText(curr_new.getCreateTime());
			return view;
		} else if (getItemViewType(position) == 1) {
			View view = inflater.inflate(R.layout.news_item2, null);
			TextView title = (TextView) view.findViewById(R.id.title);
			TextView source = (TextView) view.findViewById(R.id.source1);
			TextView comNum = (TextView) view.findViewById(R.id.comNum);
			ImageView img = (ImageView) view.findViewById(R.id.new_img);
			Article curr_new = mList.get(position);
			title.setText(curr_new.getTitle());
			source.setText(curr_new.getSource());
			comNum.setText(curr_new.getCommentNum());
			bitmapUtils.display(img, pre + curr_new.getImageUrl()[0]);
			return view;
		} else {
			View view = inflater.inflate(R.layout.news_item1, null);
			TextView title = (TextView) view.findViewById(R.id.title);
			TextView source = (TextView) view.findViewById(R.id.source1);
			TextView comNum = (TextView) view.findViewById(R.id.comNum);
			TextView time = (TextView) view.findViewById(R.id.time);
			ImageView img1 = (ImageView) view.findViewById(R.id.img1);
			ImageView img2 = (ImageView) view.findViewById(R.id.img2);
			ImageView img3 = (ImageView) view.findViewById(R.id.img3);
			Article curr_new = mList.get(position);
			title.setText(curr_new.getTitle());
			source.setText(curr_new.getSource());
			comNum.setText(curr_new.getCommentNum());
			time.setText(curr_new.getCreateTime());
			bitmapUtils.display(img1, pre + curr_new.getImageUrl()[0]);
			bitmapUtils.display(img2, pre + curr_new.getImageUrl()[1]);
			bitmapUtils.display(img3, pre + curr_new.getImageUrl()[2]);
			return view;
		}
	}

	/**
	 * ViewHolder类
	 */
	static class ViewHolder {
		TextView title;
		TextView time;
	}
}
